﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ePlanet.SingleWell.Services;
using DreamBuilder.Core.Container;

namespace Web.WebDoc
{
    public partial class WebDoc : PageBase
    {
        #region 页面参数
        /// <summary>
        /// 附件ID
        /// </summary>
        private string m_AttachmentID
        {
            get
            {
                return this.GetAttachmentID();
            }
        }

        /// <summary>
        /// 所需要跳转页码
        /// </summary>
        public int m_PageNum
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["PageNum"]))
                {
                    int pageNum = 0;
                    if (!int.TryParse(Request.QueryString["PageNum"], out pageNum))
                    {
                        this.ThrowMyException("参数PageNum不是合法的页码数字");
                    }

                    return pageNum;
                }
                else
                {
                    return 0;
                }
            }
        }

        public string m_AttachmentDBHelperString
        {
            get
            {
                return Request.QueryString["AttachmentDBHelperString"];
            }
        }

        /// <summary>
        /// 是否需要跳转页面
        /// </summary>
        private bool m_NeedJump
        {
            get;
            set;
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CheckQueryStrings())
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<script language=\"javascript\" type=\"text/javascript\">");
                    sb.Append("function addPDFSupport()");
                    sb.Append("{");
                    sb.Append("TANGER_OCX_OBJ = document.all(\"TANGER_OCX\");");
                    sb.Append("TANGER_OCX_OBJ.AddDocTypePlugin(\".pdf\", \"PDF.NtkoDocument\", \"4.0.0.0\", \"./cab/ntkopdfdoc.cab\", 51, true);");
                    sb.Append("TANGER_OCX_OBJ.OpenFromURL('WebDocHandler.ashx?" + this.CollectQueryString() + "',1);");
                    if (this.m_NeedJump)
                    {
                        sb.Append("TANGER_OCX_OBJ.GotoPDFPage(" + this.m_PageNum.ToString() + ");");
                    }
                    sb.Append("TANGER_OCX_OBJ.FullScreenMode= true;");
                    sb.Append("}");
                    sb.Append("</script>");
                    Page.RegisterClientScriptBlock("addPDFSupport", sb.ToString());
                }
                else
                {
                    this.ThrowMyException("文档参数验证失败");
                }
            }
        }

        #region 私有方法
        /// <summary>
        ///检测参数是否合法
        /// </summary>
        /// <returns></returns>
        private bool CheckQueryStrings()
        {
            if (string.IsNullOrEmpty(this.m_AttachmentID))
            {
                this.ThrowMyException("参数AttachmentID不能为空！");
            }

            if (string.IsNullOrEmpty(this.m_AttachmentDBHelperString))
            {
                this.ThrowMyException("参数AttachmentDBHelperString不能为空");
            }

            if (this.m_PageNum > 1)
            {
                this.m_NeedJump = true;
            }
            return true;
        }

        private string CollectQueryString()
        {
            string rtnQueryString = "AttachmentDBHelperString=" + this.m_AttachmentDBHelperString;
            //添加附件ID
            if (!string.IsNullOrEmpty(this.m_AttachmentID))
            {
                rtnQueryString += "&AttachmentID=" + this.m_AttachmentID;
            }

            return rtnQueryString;
        }

        private void ThrowMyException(string exceptionMessage)
        {
            Response.Clear();
            Response.Write(exceptionMessage);
            Response.End();
        }

        private string GetAttachmentID()
        {
            string rtnString = string.Empty;
            if (!string.IsNullOrEmpty(Request.QueryString["AttachmentID"]))
            {
                rtnString = Request.QueryString["AttachmentID"];
            }
            else
            {
                if (Request.QueryString["AttachmentDBHelperString"].ToLower() == "SingleWell".ToLower() && (!string.IsNullOrEmpty(Request.QueryString["DetailID"])))
                {
                    ISingleWellService oISingleWellService = UContainer.Resolve<ISingleWellService>();
                    rtnString = oISingleWellService.GetAttachIDByDetailID(Request.QueryString["DetailID"], Request.QueryString["AttachmentDBHelperString"]);
                    if (string.IsNullOrEmpty(rtnString))
                    {
                        rtnString = Request.QueryString["DetailID"];
                    }
                }
            }

            return rtnString;
        }
        #endregion

    }
}